import {observable, action} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from "../../util/reportUtil";
import {ReportStore} from "./ReportStore";

import isEmpty from 'lodash/isEmpty';
import split from 'lodash/split';

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 人员办理时间分析报表
 */
export class HandleRequestAnalyseStore {
    @observable loading = false;

    labels = {};
    @observable advanceSearchData = {};

    @observable advanceSearchPara = {
        orgtype: 3,
        orgid: '',
        datarange: 1,
        dataid: '',
        dateType: 1,
        startDate: '',
        endDate: '',
        tabKey: '1'
    }

    //modal
    @observable modalPara = {
        modalVisible: false,
        modalKey: '',
        modaltitle: '',
        dataParams: {},
        tabDatas: []
    }

    @observable analyseData = {};

    constructor() {
        this.initDatas = this.initDatas.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.showModal = this.showModal.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas() {
        API_WFREPORT.getHandleReportSearchCondition({}).then((data) => {
            const {noRight = false, rightKey = '-100', labels = {}, tabDatas = [], config = [], bottomInfo, reportid} = data;
            this.advanceSearchData = {noRight, tabDatas, config, bottomInfo, reportid};
            if (!noRight) {
                this.labels = labels;
                this.updateAdvanceSearchPara({rightKey, tabKey: 1});
            }
        });
    }

    loadAnalyseData() {
        this.loading = true;
        Promise.all([
            API_WFREPORT.getHandleReportCountData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getHandleReportEchartData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(() => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        let iddoupdate = false;
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                iddoupdate = true;
                if (o === 'orgtype') {
                    const orgtype = para[o];
                    if (orgtype > 1) {
                        const {config = []} = this.advanceSearchData;
                        if (config.length > 0) {
                            const {selectLinkageDatas = {}} = config[0];
                            defaultInfo.orgid = ReportUtil.getDefaultId(orgtype, selectLinkageDatas);
                        }
                    }
                }
                if (o === 'datarange' && para[o] > 1) isload = false;
                if (o === 'datarange' && para[o] == 1) {
                    defaultInfo.dataid = '';
                }
                if (o === 'orgid' && para[o] == '') isload = false;
                if (o === 'dataid' && para[o] == '' && (para.datarange > 1 || (para.datarange > 1 && this.advanceSearchPara.datarange > 1))) {
                    isload = false;
                }
                if (o === 'dateType') {
                    if (para[o] == 6) {
                        isload = false;
                    } else {
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
            }
        }
        if (iddoupdate) {
            this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
            if (isload) {
                this.loadAnalyseData();
            }
        }
    }

    //type 1:更多 2:echart
    showModal(type, para = {}) {
        const {reportid} = this.advanceSearchData;
        const {tabKey = 1} = this.advanceSearchPara;
        if (type == 1) {
            const echartTitle = this.labels[`echartTitle${tabKey}`];
            const dataParams = {
                'api': 'getHandleEchartMore',
                ...this.advanceSearchPara,
                tabKey
            }
            const clickParams = {'analyseType': tabKey};
            this.modalPara = {
                modalVisible: true,
                modaltitle: echartTitle,
                dataParams: dataParams,
                isRowClick: true,
                clickParams
            }
        } else if (type == 2) {
            const {analyseType = '1', key, title = ''} = para;
            const modalLabelPrefix = this.labels[`lebelsuffix${analyseType}`];
            const dataParams = {
                'api': 'getHandleReportDetailList',
                'analyseType': analyseType,
                'keyid': key,
                ...this.advanceSearchPara,
                parentOrg
            }

            let parentOrg = `${analyseType}_${key}`;
            const bottomBtns = [];
            let btnTitle = '';
            let btnClickAnalyseType = '';
            if (analyseType == 3 || analyseType == 2) {
                btnTitle = getLabel(384557, '流程耗时分析');
                btnClickAnalyseType = 1;
            } else if (analyseType == 1) {
                btnTitle = getLabel(383302, '具体流程清单');
                btnClickAnalyseType = 0;
            }
            if (btnTitle) {
                bottomBtns.push({
                    btnTitle,
                    title,
                    'parentKeyid': key,
                    'analyseType': btnClickAnalyseType,
                    'currentAnalyseType': analyseType,
                    'parentOrg': `${analyseType}_${key}`,
                    'btnClick': true,
                    parentOrg
                });
            }
            const clickParams = {'analyseType': (Number(analyseType) - 1), 'currentAnalyseType': analyseType,parentOrg};
            this.modalPara = {
                modalVisible: true,
                modaltitle: `${modalLabelPrefix}：${title}`,
                isRowClick: true,
                dataParams,
                bottomBtns,
                clickParams
            }
        }
    }

    updateModalPara(para = {}) {
        let {
            keyid = '', title = '', parentKeyid = '', analyseType = 1, currentAnalyseType = '',
            operatorid = '', currentTime, isLoadVersionTab = 0, btnClick
        } = para;

        let parentOrg = this.getParentOrgId(para);
        let dataParams = {
            'api': 'getHandleReportDetailList',
            ...this.advanceSearchPara,
            keyid,
            parentKeyid,
            analyseType,
            operatorid,
            currentTime,
            parentAnalyseType: currentAnalyseType,
            isLoadVersionTab,
            parentOrg
        };
        let modaltitle = '';
        if (analyseType > 0) {
            const modalTitlePrefix = this.labels[`lebelsuffix${analyseType}`];
            modaltitle = `${modalTitlePrefix}：${title}`;
        } else if (analyseType == 0) {
            modaltitle = getLabel(383302, '具体流程清单');
        } else if (analyseType == -1) {
            const modalTitlePrefix = getLabel(384558, '节点耗时分析');
            modaltitle = `${modalTitlePrefix}：${title}`;
        }
        const isRowClick = analyseType > 0;
        //底部Btn
        const bottomBtns = [];
        let btnTitle = '';
        let btnClickAnalyseType = '';
        let isLoadVersionTabClickPara = 0;
        if (analyseType == 3 || analyseType == 2) {
            btnTitle = getLabel(384557, '流程耗时分析');
            btnClickAnalyseType = 1;
        } else if (analyseType == 1) {
            btnTitle = getLabel(383302, '具体流程清单');
            btnClickAnalyseType = 0;
        } else if (analyseType == 0) {
            if (!isEmpty(keyid)) {
                btnTitle = getLabel(384558, '节点耗时分析');
                btnClickAnalyseType = -1;
                isLoadVersionTabClickPara = 1;
            }
        }
        if (!isEmpty(btnTitle)) {
            let _parentKeyid = keyid;
            let _currentAnalyseType = analyseType;
            if (isEmpty(keyid)) {
                _parentKeyid = parentKeyid;
                _currentAnalyseType = currentAnalyseType;
            }
            bottomBtns.push({
                btnTitle,
                title,
                'parentKeyid': _parentKeyid,
                'operatorid': parentKeyid, //节点耗时分析专用参数
                'analyseType': btnClickAnalyseType,
                'currentAnalyseType': _currentAnalyseType,
                'isLoadVersionTab': isLoadVersionTabClickPara,
                'btnClick': true,
                parentOrg
            });
        }
        let needAdvacnceSearch = analyseType == 0;
        const clickParams = {
            'analyseType': (Number(analyseType) - 1),
            'currentAnalyseType': analyseType,
            parentKeyid,
            parentOrg
        };
        const updateParam = {modaltitle, isRowClick, dataParams, clickParams, bottomBtns, needAdvacnceSearch};
        this.modalPara = {...this.modalPara, ...updateParam};
    }

    getParentOrgId(clickPara = {}) {
        let {analyseType = '', keyid, btnClick = false, parentOrg = ''} = clickPara;
        if (!btnClick) {
            let parentOrgArray = split(parentOrg, '_');
            let orgType = isEmpty(parentOrgArray) ? '' : parentOrgArray[0];
            let orgid = isEmpty(parentOrgArray) ? '' : parentOrgArray[1];
            if(analyseType >= 1) {
                orgType = analyseType;
                orgid = keyid;
                parentOrg = `${orgType}_${orgid}`;
            }
        }
        return parentOrg;
    }

    closeModal() {
        this.modalPara = {
            modalVisible: false,
            modalKey: '',
            modaltitle: '',
            dataParams: {},
            tabDatas: []
        }
    }

    clearStatus() {
        this.loading = false;
        this.advanceSearchPara = {
            orgtype: 3,
            orgid: '',
            datarange: 1,
            dataid: '',
            dateType: 1,
            startDate: '',
            endDate: '',
            tabKey: '1'
        }
        this.modalPara = {};
        this.advanceSearchData = {};
        this.analyseData = {};
    }
}